// pages/editTeam/editTeam.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

    provinceIndex: 0,
    cityIndex: 0,
    schoolIndex: 0,
    provinces: app.globalData.provinces,
    cities: ['请选择城市'],
    schools: ['请选择学校'],

    isQQorWechat: -1, /**-1代表未选择 0代表qq 1代表微信 */
    QQorWechat: "",

    typeIndex: 0,
    types: '',

    allNumber: "",
    hasNumber: "",

    title: "",
    aim: "",
    teamInfor: "",
    request: "",

    myteam:""

  },

  /**联系方式类型改变 */
  connectionChange: function (e) {
    this.setData({
      isQQorWechat: e.detail.value
    })
  },
  /**联系方式绑定 */
  connectionInput: function (e) {
    this.setData({
      QQorWechat: e.detail.value
    })
  },
  findSchoolsByCityName: function (name) {
    wx.request({
      url: 'http://' +app.globalData.ip+'/location/findSchoolsByCity',
      data: { 'cityName': name },
      success: (res) => {
        this.setData({
          schools: res.data['schools'],
          schoolIndex: 0
        })
      }
    })
  },
  /*省 市 学校 的改变*/
  changeProvince: function (e) {
    this.setData({
      provinceIndex: e.detail.value
    })
    var provinceName = this.data.provinces[this.data.provinceIndex]
    if (provinceName == '北京' || provinceName == '上海' || provinceName == '天津' || provinceName == '重庆') {
      this.setData({
        cities: [provinceName + '市'],
        cityIndex: 0,
        schoolIndex: 0,
      })
      this.findSchoolsByCityName(provinceName + '市')
    } else if (provinceName == '香港' || provinceName == '澳门' || provinceName == '台湾') {
      this.setData({
        cities: [],
        schools: [],
        cityIndex: 0,
        schoolIndex: 0
      })
    } else {
      wx.request({
        url: 'http://' + app.globalData.ip +'/location/findCitiesByProvince',
        data: { 'provinceName': this.data.provinces[this.data.provinceIndex] },
        success: (res) => {
          this.setData({
            cities: res.data['cities'],
            cityIndex: 0,
            schoolIndex: 0
          })
        }
      })
    }

  },
  changeCity: function (e) {
    this.setData({
      cityIndex: e.detail.value
    })
    this.findSchoolsByCityName(this.data.cities[this.data.cityIndex])
  },
  changeSchool: function (e) {
    this.setData({
      schoolIndex: e.detail.value
    })
  },
  /**组队类型的改变 */
  changeType: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  /**将输入的总人数赋值给data中的allNumber */
  allNumberInput: function (e) {
    this.setData({
      allNumber: e.detail.value
    })
  },
  /**将输入的已有人数赋值给data中的hasNumber */
  hasNumberInput: function (e) {
    this.setData({
      hasNumber: e.detail.value
    })
  },
  titleInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  aimInput: function (e) {
    this.setData({
      aim: e.detail.value
    })
  },
  teamInforInput: function (e) {
    this.setData({
      teamInfor: e.detail.value
    })
  },
  requestInput: function (e) {
    this.setData({
      request: e.detail.value
    })
  },

  alertInformation(text) {
    wx.showModal({
      content: text,
    })
  },
  mysubmit: function () {

    var all = parseInt(this.data.allNumber)
    var has = parseInt(this.data.hasNumber)

    /**1、检查各条件有没有填写并且判断人数关系 */
    if (this.data.schoolIndex == 0) {
      this.alertInformation("请选择学校")
    } else if (this.data.isQQorWechat == -1) {
      this.alertInformation("请选择联系方式")
    } else if (this.data.QQorWechat == '') {
      this.alertInformation("请填写联系方式")
    } else if (this.data.typeIndex == 0) {
      this.alertInformation("请选择组队类型")
    } else if (this.data.title == "") {
      this.alertInformation("请填写组队标题")
    } else if (all != 0 && has >= all) {
      this.alertInformation("已有人数必须小于总人数")
    } else {
      /**2.1发送请求到后端，存储发布的信息 */
      var sendData = {}
      sendData['teamID'] = this.data.myteam.teamID
      sendData['schoolName'] = this.data.schools[this.data.schoolIndex]
      sendData['typeName'] = this.data.types[this.data.typeIndex]
      sendData['userID'] = app.globalData.userInfo['openId']
      sendData['AllNumber'] = this.data.allNumber
      sendData['hasNumber'] = this.data.hasNumber
      sendData['title'] = this.data.title
      sendData['qqorWechat'] = this.data.isQQorWechat == '1' ? true : false,
      sendData['qqorWechatText'] = this.data.QQorWechat
      sendData['aim'] = this.data.aim
      sendData['teamInfo'] = this.data.teamInfor
      sendData['Request'] = this.data.request
      
      wx.request({
        url: 'http://' + app.globalData.ip +'/team/updateTeam',
        data: { userID:app.globalData.userInfo.openId,team: JSON.stringify(sendData) },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          if(res.data.msg==1){
            /**2.2存储后刷新当前页面 */
            this.inputInit();
            /**2.3弹出提示信息表示成功 */
            wx.showToast({
              title: '更新成功',
            })
          }else{
            wx.showModal({
              content: '更新失败',
            })
          }
          
        },
        fail: function () {
          wx.showModal({
            title: '前端出错',
            content: '发送请求失败',
          })
        }
      })

    }

  },

  /**提交后初始化 */
  inputInit: function () {
    this.setData({
      provinceIndex: 0,
      cityIndex: 0,
      schoolIndex: 0,
      provinces: app.globalData.provinces,
      cities: ['请选择城市'],
      schools: ['请选择学校'],

      isQQorWechat: -1, /**-1代表未选择 0代表qq 1代表微信 */
      QQorWechat: "",
      typeIndex: 0,
      types: app.globalData.types,

      allNumber: "",
      hasNumber: "",

      title: "",
      aim: "",
      teamInfor: "",
      request: ""
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /**初始化各选项为编辑的选项 */
    this.setData({
      provinces: app.globalData.provinces,
      types: app.globalData.types,
      myteam: app.globalData.myteam
    })

    var team = this.data.myteam
    var tindex=0;
    /*寻找编辑的组队类型 在 类型组 中的下标*/ 
    for(var i=0;i<this.data.types.length;++i){
      if(team.typeName == this.data.types[i]){
        tindex = i;
        break;
      }
    }
    this.setData({      
      isQQorWechat:team.qqorWechat,
      QQorWechat:team.qqorWechatText,
      typeIndex: tindex,  
      allNumber:team.allNumber,
      hasNumber:team.hasNumber,
      title:team.title,
      aim:team.aim,
      teamInfor:team.teamInfo,
      request:team.request
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})